use salvo::Request;
use anyhow::{Result, Context};

pub fn get_tenant_id(req: &Request) -> Result<Option<i64>> {
    // 从请求头获取租户ID
    if let Some(tenant_id) = req.headers().get("x-tenant-id") {
        let tenant_id = tenant_id.to_str()?.parse::<i64>()
            .context("Failed to parse tenant_id from header")?;
        return Ok(Some(tenant_id));
    }

    // 从查询参数获取租户ID
    if let Some(tenant_id) = req.query::<String>("tenant_id") {
        let tenant_id = tenant_id.parse::<i64>()
            .context("Failed to parse tenant_id from query")?;
        return Ok(Some(tenant_id));
    }

    // 找不到tenant_id时返回None
    Ok(None)
}
